package SetMap;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @TODO
 * @Author xu
 * 2025/3/17
 */
public class Remove {
    public static void main(String[] args) {
        Person[] persons = new Person[] {
                new Person("Alice", 30),
                new Person("Bob", 25),
                new Person("Alice", 30), // 重复的对象
                new Person("Charlie", 35),
                new Person("Bob", 25),   // 重复的对象
                new Person("David", 40),
        };

        // 移除重复的Person对象
        Person[] uniquePersons = removeDuplicates(persons);

        // 输出结果
        System.out.println("不重复的Person对象:");
        for (Person person : uniquePersons) {
            System.out.println(person);
        }
    }

    private static Person[] removeDuplicates(Person[] persons) {
        Set<Person> personSet = new HashSet<>(Arrays.asList(persons)); // 利用HashSet去重
        return personSet.toArray(new Person[0]); // 将结果转换为Person数组
    }
}
